package ControlLayer;

import java.util.Date;
import java.util.ArrayList;

import DBLayer.*;
import ModelLayer.*;

public class SearchControler implements IFSearch {
	private IFDBContact contactDB;
	private IFDBEmployee employeeDB;
	private IFDBProduct productDB;
	private IFDBSalesOrder salesOrderDB;
	private IFDBDelivery deliveryDB;
	private IFDBSalesLine salesLineDB;
	
	public SearchControler()
	{
		contactDB = new DbContact();
		employeeDB = new DbEmployee();
		productDB = new DbProduct();
		salesOrderDB = new DbSalesOrder(); 
		deliveryDB = new DbDelivery();
		salesLineDB = new DbSalesLine();
	}

	@Override
	public ArrayList<SalesOrder> getAllSalesOrders() {
		ArrayList<SalesOrder> getAll = new ArrayList<SalesOrder>();
		getAll = salesOrderDB.getAllSalesOrder();
		return getAll;
	}

	@Override
	public SalesOrder getSalesOrderById(int salesOrderId) {
		return salesOrderDB.getSalesOrderById(salesOrderId);
	}

	@Override
	public ArrayList<SalesOrder> getSalesOrdersByCustomerId(int customerId) {
		return salesOrderDB.getSalesOrderByCustomerId(customerId);
	}

	@Override
	public ArrayList<SalesOrder> getSalesOrdersByCustomerName(String customerName) {
		ArrayList<Contact> customers = contactDB.getCustomerByName(customerName);
		ArrayList<SalesOrder> temp = new ArrayList<SalesOrder>();
		ArrayList<SalesOrder> results = new ArrayList<SalesOrder>();
		for(int i=0; i<customers.size(); i++)
		{
			temp = salesOrderDB.getSalesOrderByCustomerId(customers.get(i).getId());
			for(int j=0;j<temp.size();j++)
			{
				results.add(temp.get(j));
			}
		}
		return results;
	}

	@Override
	public ArrayList<SalesOrder> getSalesOrdersByStatus(String status) {
		return salesOrderDB.getSalesOrdersByStatus(status);
	}

	@Override
	public ArrayList<SalesOrder> getSalesOrdersByEmployeeId(int employeeId) {
		return salesOrderDB.getSalesOrderByEmployeeId(employeeId);
	}

	@Override
	public ArrayList<SalesOrder> getSalesOrdersByEmployeeName(String employeeName) {
		ArrayList<Employee> employees = employeeDB.getEmployeeByName(employeeName);
		ArrayList<SalesOrder> temp = new ArrayList<SalesOrder>();
		ArrayList<SalesOrder> results = new ArrayList<SalesOrder>();
		for(int i=0; i<employees.size(); i++)
		{
			temp = salesOrderDB.getSalesOrderByEmployeeId(employees.get(i).getId());
			for(int j=0;j<temp.size();j++)
			{
				results.add(temp.get(j));
			}
		}
		return results;
	}

	@Override
	public ArrayList<SalesOrder> getSalesOrdersByDeliveryId(int deliveryId) {
		return salesOrderDB.getSalesOrdersByDeliveryId(deliveryId);
	}

	@Override
	public ArrayList<Contact> getAllCustomers() {
		return contactDB.getAllCustomers();
	}

	@Override
	public ArrayList<Contact> getAllPrivateCustomers() {
		return contactDB.getAllPrivateCustomers();
	}

	@Override
	public ArrayList<Contact> getAllBusinessCustomers() {
		return contactDB.getAllBusinessCustomers();
	}

	@Override
	public Contact getCustomerById(int customerId) {
		return contactDB.getCustomerById(customerId);
	}

	@Override
	public ArrayList<Contact> getCustomersLikeName(String customerName) {
		return contactDB. getCustomerByName(customerName);
	}

	@Override
	public ArrayList<Contact> getArchivedCustomers() {
		return contactDB.getArchivedCustomers();
	}

	@Override
	public ArrayList<Contact> getArchivedCustomersLikeName(String customerName) {
		return contactDB.getArchivedCustomersByName(customerName);
	}

	@Override
	public ArrayList<Employee> getAllEmployees() {
		return employeeDB.getAllEmployees();
	}

	@Override
	public Employee getEmployeeById(int employeeId) {
		return employeeDB.getEmployeeById(employeeId);
	}

	@Override
	public ArrayList<Employee> getEmployeesLikeName(String employeeName) {
		ArrayList<Employee> likeName = new ArrayList<Employee>();
		likeName = employeeDB.getEmployeeByName(employeeName);
		return likeName;
	}

	@Override
	public ArrayList<Employee> getEmployeesByRole(String role) {
		return employeeDB.getEmployeesByRole(role);
	}

	@Override
	public ArrayList<Employee> getArchivedEmployees() {
		return employeeDB.getArchivedEmployees();
	}

	@Override
	public ArrayList<Employee> getArchivedEmployeesLikeName(String employeeName) {
		return employeeDB.getArchivedEmployeesByName(employeeName);
	}

	@Override
	public ArrayList<Delivery> getAllDelivery() {
		ArrayList<Delivery> getAll = new ArrayList<Delivery>();
		getAll = deliveryDB.getAllDelivery();
		return getAll;
	}

	@Override
	public Delivery getDeliveryById(int deliveryId) {
		return deliveryDB.getDeliveryById(deliveryId);
	}

	@Override
	public ArrayList<Delivery> getDeliveriesOnDate(Date deliveryDate) {
		ArrayList<Delivery> getAll = new ArrayList<Delivery>();
		getAll = deliveryDB.getAllDelivery();
		return getAll;
	}

	@Override
	public ArrayList<Delivery> getDeliveriesByStatus(String status) {
		return deliveryDB.getDeliveryByStatus(status);
	}

	@Override
	public Employee getMostProductiveEmployee() {
		ArrayList<Employee> employees = new ArrayList<Employee>();
		ArrayList<SalesOrder> orders = new ArrayList<SalesOrder>();
		int bestEmployeeIndex=0;
		double max = 0.0;
		employees = employeeDB.getAllEmployees();
		for(int i=0;i<employees.size();i++)
		{
			orders = salesOrderDB.getSalesOrderByEmployeeId(employees.get(i).getId());
			if(orders.size()>max)
			{
				max=orders.size();
				bestEmployeeIndex=i;
			}
		}
		return employees.get(bestEmployeeIndex);
	}

	@Override
	public Contact getBestCustomer() {
		ArrayList<Contact> customers = new ArrayList<Contact>();
		ArrayList<SalesOrder> orders = new ArrayList<SalesOrder>();
		int bestCustomerIndex=0;
		double max = 0.0;
		double sum;
		customers = contactDB.getAllCustomers();
		for(int i=0;i<customers.size();i++)
		{
			sum = 0.0;
			orders = salesOrderDB.getSalesOrderByCustomerId(customers.get(i).getId());
			for(int j=0; j<orders.size();j++)
			{
				sum = sum + orders.get(j).getTotal();
			}
			if(sum>max)
			{
				max=sum;
				bestCustomerIndex=i;
			}
		}
		return customers.get(bestCustomerIndex);
	}

	@Override
	public Product getMostSoldProduct() {
		ArrayList<Product> products = new ArrayList<Product>();
		ArrayList<SalesLine> salesLines = new ArrayList<SalesLine>();
		int bestProductIndex=0;
		double max = 0.0;
		double sum;
		products = productDB.getAllProducts();
		for(int i=0;i<products.size();i++)
		{
			sum = 0.0;
			salesLines = salesLineDB.getSalesLineByProduct(products.get(i).getId());
			for(int j=0; j<salesLines.size();j++)
			{
				sum = sum + salesLines.get(j).getQuantity();
			}
			if(sum>max)
			{
				max=sum;
				bestProductIndex=i;
			}
		}
		return products.get(bestProductIndex);
	}

	@Override
	public ArrayList<Product> checkStock() {
		ArrayList<Product> products = new ArrayList<Product>();
		ArrayList<Product> results = new ArrayList<Product>();
		products = productDB.getAllProducts();
		for(int i=0;i<products.size();i++)
		{
			if(products.get(i).getStockAmount() <= products.get(i).getMinStock())
				results.add(products.get(i));
		}
		return results;
	}



	@Override
	public ArrayList<Product> getAllProducts() {
		return productDB.getAllProducts();
	}



	@Override
	public Product getProductById(int productId) {
		return productDB.getProductById(productId);
	}
	
	

	@Override
	public ArrayList<Product> getProductsLikeName(String productName) {
		return productDB.getProductsByName(productName);
	}



	@Override
	public ArrayList<Product> getProductsInStock() {
		ArrayList<Product> products = new ArrayList<Product>();
		ArrayList<Product> results = new ArrayList<Product>();
		products = productDB.getAllProducts();
		for(int i=0;i<products.size();i++)
		{
			if(products.get(i).getStockAmount() > products.get(i).getMinStock())
				results.add(products.get(i));
		}
		return results;
	}



	@Override
	public ArrayList<Product> getProductsOutOfStock() {
		ArrayList<Product> products = new ArrayList<Product>();
		ArrayList<Product> results = new ArrayList<Product>();
		products = productDB.getAllProducts();
		for(int i=0;i<products.size();i++)
		{
			if(products.get(i).getStockAmount() == 0)
				results.add(products.get(i));
		}
		return results;
	}



	@Override
	public ArrayList<Product> getArchivedProducts() {
		return productDB.getArrachivedProducts();
	}



	@Override
	public ArrayList<Product> getArchivedProductsLikeName(String productName) {
		return productDB.getArchivedProductsByName(productName);
	}

	@Override
	public ArrayList<SalesOrder> getSalesOrdersByCustomerAndStatus(
			int customerId, String status) {
		ArrayList<SalesOrder> results = new ArrayList<SalesOrder>();
		ArrayList<SalesOrder> orders = new ArrayList<SalesOrder>();
		orders = salesOrderDB.getSalesOrderByCustomerId(customerId);
		for(int i=0;i<orders.size();i++)
		{
			if(orders.get(i).getStatus().equals(status))
				results.add(orders.get(i));
		}
		return results;
	}

	@Override
	public ArrayList<Contact> getCustomersByNameAndType(String customerName,
			String type) {
		ArrayList<Contact> results = new ArrayList<Contact>();
		if(type.toLowerCase().equals("private")) {
			results = contactDB.getPrivateCustomersByName(customerName);
		}
		else {
			if(type.toLowerCase().equals("business")) {
				results = contactDB.getBusinessCustomersByName(customerName);
			}
		}
		return results;	
	}

	@Override
	public ArrayList<Employee> getEmployeeByNameAndRole(String emplyeeName,
			String role) {
		ArrayList<Employee> employees = new ArrayList<Employee>();
		ArrayList<Employee> results = new ArrayList<Employee>();
		employees = employeeDB.getEmployeeByName(emplyeeName);
		for(int i=0;i<employees.size();i++)
		{
			if(employees.get(i).getRole().equals(role.toLowerCase()))
				results.add(employees.get(i));
		}
		return results;
	}

	@Override
	public ArrayList<Contact> getAllArchivedContacts() {
		ArrayList<Contact> results = new ArrayList<Contact>();
		ArrayList<Employee> employees = new ArrayList<Employee>();
		ArrayList<Contact> customers = new ArrayList<Contact>();
		employees = getArchivedEmployees();
		customers = getArchivedCustomers();
		for(int i=0; i<customers.size(); i++)
		{
			results.add(customers.get(i));
		}
		for(int i=0; i<employees.size(); i++)
		{
			results.add(employees.get(i));
		}
		return results;
	}

	@Override
	public ArrayList<Contact> getAllArchivedContactsLikeName(String contactName) {
		ArrayList<Contact> results = new ArrayList<Contact>();
		ArrayList<Employee> employees = new ArrayList<Employee>();
		if(getArchivedCustomersLikeName(contactName).size()!=0)
		{
			results=getArchivedCustomersLikeName(contactName);
		}
		else
		{
			employees=getArchivedEmployeesLikeName(contactName);
			for(int i=0;i<employees.size();i++)
			{
				results.add(employees.get(i));
			}
		}
		return results;
	}
}
